
package thelife;

/**
 *
 * @author Paweł Młynarczyk <zwarios@gmail.com>
 */
public class Matrix {
    boolean[][] present;
    boolean[][] future;
    int h;
    int w;
    Matrix(int h, int w){
        this.h = h;
        this.w = w;
        this.present = new boolean[h][w];
        this.future = new boolean[h][w];
    }
    
    // macierz[i][j]
    int livingNeighbours(int i, int j){
        int ln = 0;
        if(i - 1 >= 0 && j - 1 >= 0 && present[i - 1][j - 1])
            ln += 1;
        if(i - 1 >= 0 && present[i - 1][j])
            ln += 1;
        if(i - 1 >= 0 && j + 1 < w && present[i - 1][j + 1])
            ln += 1;
        if(j - 1 >= 0 && present[i][j - 1])
            ln += 1;
        if(j + 1 < w && present[i][j + 1])
            ln += 1;
        if(i + 1 < h && j - 1 >= 0 && present[i + 1][j - 1])
            ln += 1;
        if(i + 1 < h && present[i + 1][j])
            ln += 1;
        if(i + 1 < h && j + 1 < w && present[i + 1][j + 1])
            ln += 1;
        return ln;
    }
    boolean[][] nextEpoch(){
        Cell[][] cells = new Cell[h][w];
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(present[i][j]){
                    if(livingNeighbours(i, j) == 2 || livingNeighbours(i, j) == 3)
                        future[i][j] = true;
                    else
                        future[i][j] = false;
                } else {
                    if(livingNeighbours(i, j) == 3)
                        future[i][j] = true;
                    else
                        future[i][j] = false;
                }
//                cells[i][j] = new Cell(this, i, j);
//                cells[i][j].start();
            }
        }
//        for(int i = 0; i < h; i++)
//            for(int j = 0; j < w; j++)
//                cells[i][j].join();
        for(int i = 0; i < h; i++){
            System.arraycopy(future[i], 0, present[i], 0, w);
        }
        //present = future.clone();
        return present;
    }
    void insertLife(int x, int y){
        present[x][y] = true;
    }
    void clear(){
        this.present = new boolean[50][50];
    }
    void randomize(){
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(Math.random() > 0.3){
                    present[i][j] = true;
                } else {
                    present[i][j] = false;
                }
            }
        }
    }
    
    void drawShape(int shape){
        clear();
        switch(shape){
            case 0:
                present[24][23] = true;
                present[25][24] = true;
                present[23][25] = true;
                present[24][25] = true;
                present[25][25] = true;
                break;
            case 1:
                present[23][24] = true;
                present[24][23] = true;
                present[24][24] = true;
                present[24][25] = true;
                present[25][23] = true;
                present[25][25] = true;
                present[26][24] = true;
                break;
            case 2:
                present[22][22] = true;
                present[22][24] = true;
                present[22][26] = true;
                present[23][22] = true;
                present[23][26] = true;
                present[24][22] = true;
                present[24][26] = true;
                present[25][22] = true;
                present[25][26] = true;
                present[26][22] = true;
                present[26][24] = true;
                present[26][26] = true;
                break;
            case 3:
                present[9][7] = true;
                present[9][8] = true;
                present[10][7] = true;
                present[10][8] = true;
                present[9][16] = true;
                present[9][17] = true;
                present[10][15] = true;
                present[10][17] = true;
                present[11][15] = true;
                present[11][16] = true;
                present[11][23] = true;
                present[11][24] = true;
                present[12][23] = true;
                present[12][25] = true;
                present[13][23] = true;
                
                present[9-2][16+12] = true;
                present[9-2][17+12] = true;
                present[10-2][15+12] = true;
                present[10-2][17+12] = true;
                present[11-2][15+12] = true;
                present[11-2][16+12] = true;
                present[11-2][23+12] = true;
                present[11-2][24+12] = true;
                present[12-2][23+12] = true;
                present[12-2][25+12] = true;
                present[13-2][23+12] = true;
             
                present[19][29] = true;
                present[19][30] = true;
                present[19][31] = true;
                present[20][29] = true;
                present[21][30] = true;
                
                present[7][39] = true;
                present[7][40] = true;
                present[8][39] = true;
                present[8][40] = true;
                break;
        }
    }
}
